#!/usr/bin/env bash

TOOLCHAIN=`pwd`/node_modules/nodeos-cross-toolchain

source $TOOLCHAIN/scripts/adjustEnvVars.sh || exit $?


#
# Build each layer up
#

( cd node_modules/nodeos-barebones && npm run build ) &&
( cd node_modules/nodeos-initramfs && npm run build ) &&
( cd node_modules/nodeos-rootfs    && npm run build ) &&
( cd node_modules/nodeos-usersfs   && npm run build ) || err $?


#
# Wrap the system up and pack it
#

OUT_DIR=`pwd`/out/$CPU_FAMILY/$PLATFORM
mkdir -p $OUT_DIR
case $PLATFORM in
  *_qemu|docker|vagga)
    cp node_modules/nodeos-barebones/out/latest $OUT_DIR/barebones
    cp node_modules/nodeos-initramfs/out/latest $OUT_DIR/initramfs
  ;;

  *_iso|*_img)
    cp node_modules/nodeos-rootfs/out/latest $OUT_DIR/rootfs
  ;;

  *)
    echo -e "${RED}Unknown platform '$PLATFORM'${CLR}"
    exit 10
  ;;
esac
cp node_modules/nodeos-usersfs/out/latest $OUT_DIR/usersfs

ln -sfn $CPU_FAMILY/$PLATFORM out/latest
